package cn.kevenpotter.leetcode.easy.e682;

import java.util.Deque;
import java.util.LinkedList;

public class CalPoints {

    public static void main(String[] args) {
        System.out.println(calPoints(new String[]{"5", "2", "C", "D", "+"}));
    }

    public static int calPoints(String[] ops) {
        Deque<Integer> stack = new LinkedList<Integer>();
        for (int i = 0; i < ops.length; i++) {
            switch (ops[i]) {
                case "+":
                    int first = stack.pop();
                    int second = stack.pop();
                    stack.push(second);
                    stack.push(first);
                    stack.push(first + second);
                    continue;
                case "D":
                    stack.push(Integer.valueOf(stack.peek() * 2));
                    continue;
                case "C":
                    stack.pop();
                    continue;
                default:
                    stack.push(Integer.valueOf(ops[i]));
            }
        }
        int sum = 0;
        while (!stack.isEmpty()) {
            sum += stack.pop();
        }
        return sum;
    }
}
